package com.kanshu.ksgb.fastread.doudou.common.util;

import android.support.annotation.NonNull;
import android.util.Log;

import java.lang.ref.WeakReference;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

/**
 * Title:
 * Description: RxJava2.x实现定时器
 * Copyright: Copyright(c)2018
 * Company: 看书网
 * CreateTime: 2018/10/12 11:47
 *
 * @author EvanHe
 * @version 3.0
 */
public class RxTimerUtils {
    private static WeakReference<Disposable> mWeak;

    /**
     * seconds秒后执行next操作
     *
     * @param seconds 间隔秒
     * @param next    执行回调
     */
    public static void timer(long seconds, final IRxNext next) {
        Observable.timer(seconds, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable disposable) {
                        mWeak = new WeakReference<>(disposable);
                    }

                    @Override
                    public void onNext(@NonNull Long number) {
                        if (next != null) {
                            next.doNext(number);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        cancel();
                    }

                    @Override
                    public void onComplete() {
                        cancel();
                    }
                });
    }

    /**
     * 每隔seconds秒秒后执行next操作
     *
     * @param seconds 延迟执行以及间隔时间
     * @param next    执行回调
     */
    public static void interval(long seconds, final IRxNext next) {
        Observable.interval(seconds, seconds, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable disposable) {
                        mWeak = new WeakReference<>(disposable);
                    }

                    @Override
                    public void onNext(@NonNull Long number) {
                        if (next != null) {
                            next.doNext(number);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }


    /**
     * 取消订阅
     */
    public static void cancel() {
        if (mWeak != null) {
            Disposable disposable = mWeak.get();
            if (disposable != null && !disposable.isDisposed()) {
                disposable.dispose();
                mWeak = null;
                Log.i("evan", "========取消定时器========");
            }
        }
    }

    public interface IRxNext {
        void doNext(long number);
    }

}
